package com.SouthernWall_404.Age_Capitalism.common.event;

import com.SouthernWall_404.Age_Capitalism.common.API.Info.StoreKeyInfo;
import com.SouthernWall_404.Age_Capitalism.common.World.Entity.StoreBlockEntity;
import com.SouthernWall_404.Age_Capitalism.common.World.Items.StoreKeyItem;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class OnItemChanged {
    // 存储玩家上一次的主手物品
    private static final Map<UUID, ItemStack> lastMainHandItems = new HashMap<>();

    // 检测玩家切换物品（通过 tick 检测）
    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
        if (event.phase != TickEvent.Phase.END) return;

        Player player = event.player;
        ItemStack currentItem = player.getMainHandItem();
        ItemStack lastItem = lastMainHandItems.get(player.getUUID());

        // 检查物品是否变化
        if (lastItem == null) {
            // 首次记录
            lastMainHandItems.put(player.getUUID(), currentItem.copy());
            return;
        }

        if (!ItemStack.matches(currentItem, lastItem)) {
            // 物品发生变化，触发事件
            onItemChanged(player, lastItem, currentItem);
            // 更新记录
            lastMainHandItems.put(player.getUUID(), currentItem.copy());
        }
    }

    private static void onItemChanged(Player player, ItemStack oldItem, ItemStack newItem) {
        // 在这里处理物品切换逻辑
//        player.displayClientMessage(
//                Component.literal("物品切换: " + oldItem.getHoverName().getString()
//                        + " → " + newItem.getHoverName().getString()),
//                true
//        );

//        ItemStack itemStack=player.getItemInHand(InteractionHand.MAIN_HAND);
        if (newItem.getItem() instanceof StoreKeyItem) {
            StoreKeyInfo storeKeyInfo = StoreKeyInfo.getFromItem(newItem);

            BlockPos targetPos = storeKeyInfo.blockPos;
            BlockPos selfPos = player.getOnPos();
//            double distance=Math.sqrt(player.distanceToSqr(new Vec3(targetPos.getX(),targetPos.getY(),targetPos.getZ())));

            boolean highlight = false;

            BlockEntity blockEntity = player.level().getBlockEntity(targetPos);
            if (blockEntity instanceof StoreBlockEntity storeBlockEntity) {
                String storeId = storeKeyInfo.uuid.toString();
                if (storeBlockEntity.uuid.equals(storeId)) {
                    highlight = true;

                }
                storeBlockEntity.setHighlighted(highlight);
            }
        }
        if(oldItem.getItem()instanceof StoreKeyItem)
        {
            StoreKeyInfo storeKeyInfo = StoreKeyInfo.getFromItem(oldItem);

            BlockPos targetPos = storeKeyInfo.blockPos;
            BlockPos selfPos = player.getOnPos();
//            double distance=Math.sqrt(player.distanceToSqr(new Vec3(targetPos.getX(),targetPos.getY(),targetPos.getZ())));

            boolean highlight = false;

            BlockEntity blockEntity = player.level().getBlockEntity(targetPos);
            if (blockEntity instanceof StoreBlockEntity storeBlockEntity) {
                String storeId = storeKeyInfo.uuid.toString();
                if (storeBlockEntity.uuid.equals(storeId)) {
                    highlight = false;

                }
                storeBlockEntity.setHighlighted(highlight);
            }

        }
    }
}




